package org.qloudgen.txn.dilithium.cluster.pages;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import org.qloudgen.txn.dilithium.cluster.pages.common.BasePage;
import org.qloudgen.txn.dilithium.cluster.thread.ClusterThread;
import org.qloudgen.txn.dilithium.cluster.beans.Cluster;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.database.ClusterImpl;

public class InitClusterPage extends BasePage{
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */
	static Log log = LogFactory.getLog(InitClusterPage.class);
	
	private static final long serialVersionUID = 1L;
	private transient Cluster cluster;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public InitClusterPage(PageParameters pageParameters){
		super();
		
		this.pp = pageParameters;
		final int clusterID = this.pp.get("clusterID").toInt();
		
		// form.border
		final WebMarkupContainer border = new WebMarkupContainer("border");
		border.setOutputMarkupId(true);

		// this.feedback
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		this.add(feedback);
	
		// this.form
		final Form form = new Form("form") {
			
			private static final long serialVersionUID = 1L;

			protected void onSubmit() {
				
				Cluster newCluster = (Cluster)getDefaultModelObject();
				
				cluster.setGtmMasterIp(newCluster.getGtmMasterIp());
				cluster.setGtmSlaveIp(newCluster.getGtmSlaveIp());
				cluster.setNodeIps(newCluster.getNodeIps());
				cluster.setClusterName(null);
				
				// check cluster info 
				String checkInfo = cluster.checkClusterInfo();
				
				if (checkInfo != null && !checkInfo.equals("")) {
					
					info(checkInfo);
				
				} else {
					
					StringBuffer sb = new StringBuffer();
					getSession().setAttribute("logInfo", sb);
					new ClusterThread(cluster,CommonValues.CLUSTER_INIT,sb).start();
					
					setResponsePage(ShowLogPage.class,pp);
				}
			}
			
		};
		
		form.setDefaultModel(new CompoundPropertyModel<Cluster>(new Cluster()));
		this.add(form);
		form.add(border);
		
		//get default conf from metadb
		cluster = ClusterImpl.getClusterInfo(clusterID);
				
		border.add(new RequiredTextField<String>("clusterName").setDefaultModel(new Model(cluster.getClusterName())).setEnabled(false));
		border.add(new TextField<String>("clusterDesc").setDefaultModel(new Model(cluster.getClusterDesc())).setEnabled(false));
		border.add(new RequiredTextField<String>("gtmMasterIp"));
		border.add(new TextField<String>("gtmSlaveIp"));
		border.add(new TextArea<String>("nodeIps").setRequired(true));
	
		form.add(new Button ("submit"));
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(ClusterMainPage.class,pp);
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
		
		addFooter();
		
	}

}
